package cn.edu.hitsz.infra;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import populus.core.queue.UrlItem;

/**
 * Extract common URL from the content given( Here represent the content of the
 * page)
 * 
 * @author mingchen
 * @date 2015年8月5日
 */
public class DefaultLinkExtract implements LinkExtract {
	private final String content;
	private final UrlItem reference;
	/**
	 * The constructor method
	 * 
	 * @param content
	 *            String(Content of the page)
	 * @param url
	 *            UrlItem for reference
	 * 
	 */
	public DefaultLinkExtract(String content,UrlItem url) {
		if (content.equals(null) || content.trim().length() == 0) {
			throw new IllegalArgumentException("The content cannont be null");
		}
		if(url.equals(null)||url.getUrl().trim().equals("")){
			try {
				throw new MalformedURLException("The URL cannot be null");
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
		}
		this.content = content;
		reference = url;

	}

	/**
	 * The override method from interface of LinkExtract
	 * 
	 */

	public List<String> extractLinks() {
		List<String> defaultUrls = new ArrayList<String>();
		Pattern pattern = Pattern.compile("(?i)(?s)<\\s*?a.*?href=\"(.*?)\".*?>");
		Matcher matcher = pattern.matcher(content);
		while(matcher.find()){
			String url = matcher.group(1);
			// If the url path is relatice path
			if(url.startsWith("/")){
				if(reference.getUrl().trim().endsWith("/")){
					url = reference.getUrl().trim() + url;
				}
			}
			defaultUrls.add(url);
		}
		return defaultUrls;
	}
	
	/**
	 * 
	 * Packge all the urls into UrlItems
	 * 
	 * @param urls
	 * @return List<UrlItem> 2015年8月6日
	 */
	public List<UrlItem> packgeToUrlItem(List<String> urls){
		List<UrlItem> urlitems = new ArrayList<UrlItem>();
		for(String url:urls){
			urlitems.add(new UrlItem(url,reference.getDepth()+1));
		}
		return urlitems;
	}

	/**
	 * 
	 * extract the links and then package them to UrlItems
	 * @return
	 * List<UrlItem>
	 * 2015年8月17日下午4:31:34
	 */
	public List<UrlItem> offerLinks(){
		return packgeToUrlItem(extractLinks());
	}
}
